
package cn.zniot.so.transport;

import cn.zniot.so.application.constant.Constants;
import cn.zniot.so.transport.exception.NetException;
import cn.zniot.so.transport.server.BoxAndClientServerHandler;
import cn.zniot.so.transport.server.FileServer;
import cn.zniot.so.transport.server.HttpServer;

/**
 * 通讯管理器，负责网络模块的创建和管理
 */
public class CommonManager {

    /**
     * 车机监听服务器
     */
    private IServer server = null;

    private static CommonManager instance = new CommonManager();

    private CommonManager() {

    }

    public static CommonManager getInstance() {
        return instance;
    }

    /**
     * 启动通讯模块
     */
    public void startUpBox() throws Exception {
        try {
            if (Constants.TYPE_SERVER_TCP == ServerType.TCP) {
                // 创建TCP监听服务器
                this.server = NetFacade.getInstance().createBoxServer( Constants.THREADNUM_LOGIC_SERVER,Constants.BOXPORT,
                        new BoxAndClientServerHandler());
                this.server.startup();
            }
        } catch (Exception e) {
            throw new Exception(e.getMessage(), e);
        }
    }

    /**
     * 启动客户端访问的接口
     */
    public void startUpHttp() throws Exception {
        try {
            // HttpServer server=new
            // HttpServer(Constants.THREADNUM_LOGIC_CLIENT,Constants.MGR_PORT);
            HttpServer server = new HttpServer(Constants.MGR_PORT,Constants.THREADNUM_LOGIC_SERVER);
            server.init();
            server.startup();
        } catch (Exception e) {
            throw new Exception(e.getMessage(), e);
        }
    }

    /**
     * 启动文件上传服务器
     */
    public void startUpFileUpload() throws Exception {
        try {
            FileServer s = new FileServer(Constants.DOWNFILEPORT,Constants.THREADNUM_LOGIC_SERVER);
            s.start();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw new Exception(e.getMessage(),e);
        }
    }
    
    /**
     * 关闭通讯模块
     */
    public void shutDown() throws Exception {
        try {
            this.server.shutdown();
        } catch (NetException e) {
            throw new Exception(e.getMessage(), e);
        }
    }

}
